package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.ElectricityPlacePrice;
import net.hn.hnms.biz.energy.mapper.ElectricityPlacePriceMapper;
import net.hn.hnms.biz.energy.request.ElectricityPlacePriceReq;
import net.hn.hnms.biz.energy.service.ElectricityPlacePriceService;
import net.hn.hnms.biz.energy.vo.ElectricityPlacePriceVo1;
import net.hn.hnms.biz.energy.vo.WaterSupplyCompanyVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteUserService;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ElectricityPlacePriceServiceImpl extends BaseService<ElectricityPlacePrice, ElectricityPlacePriceVo1> implements ElectricityPlacePriceService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private ElectricityPlacePriceMapper electricityPlacePriceMapper;

    @Override
    public int insert(ElectricityPlacePriceReq electricityPlacePriceReq) {
        ElectricityPlacePrice insertObj = new ElectricityPlacePrice();
        BeanUtil.copyProperties(electricityPlacePriceReq, insertObj);
        return electricityPlacePriceMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        electricityPlacePriceMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(ElectricityPlacePriceReq electricityPlacePriceReq) {
        ElectricityPlacePrice updateObj = new ElectricityPlacePrice();
        BeanUtil.copyProperties(electricityPlacePriceReq, updateObj);
        int updateCount = electricityPlacePriceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ElectricityPlacePrice selectDetailById(String id) {
        ElectricityPlacePrice electricityPlacePrice = electricityPlacePriceMapper.selectById(id);
        if (electricityPlacePrice == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return electricityPlacePrice;
    }

    @Override
    public ResponsePage<ElectricityPlacePriceVo1> selectPageList(ElectricityPlacePriceReq electricityPlacePriceReq, PageQuery pageQuery) {
        Page<ElectricityPlacePrice> page = electricityPlacePriceMapper.selectPage(pageQuery.build(), buildQuery(electricityPlacePriceReq).orderByDesc(ElectricityPlacePrice::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        List<ElectricityPlacePriceVo1> list =page.getRecords().stream().map(o ->convert(o, ElectricityPlacePriceVo1.class)).collect(Collectors.toList());
        list.forEach(ii->{
            LoginUser userInfoById = remoteUserService.getUserInfoById(Long.valueOf(ii.getCreateUserId()));
            if (Optional.ofNullable(userInfoById).isPresent()) {
                ii.setCreateUserName(userInfoById.getNickname());
                ii.setUsername(userInfoById.getNickname());
            }

        });
//        return new ResponsePage<>(page.getRecords(), page.getTotal());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public List<ElectricityPlacePrice> selectList(ElectricityPlacePriceReq electricityPlacePriceReq) {
        List<ElectricityPlacePrice> list = electricityPlacePriceMapper.selectList(buildQuery(electricityPlacePriceReq));
        return list;
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ElectricityPlacePrice> buildQuery(ElectricityPlacePriceReq req){
        return new LambdaQueryWrapper<ElectricityPlacePrice>()
                .like(StrUtil.isNotBlank(req.getElectricityPlaceName()), ElectricityPlacePrice::getElectricityPlaceName, req.getElectricityPlaceName())
                .eq(StrUtil.isNotBlank(req.getMineCode()), ElectricityPlacePrice::getMineCode, req.getMineCode())
                .ge(req.getBeginDateTime() != null, ElectricityPlacePrice::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ElectricityPlacePrice::getCreateTime, req.getEndDateTime());
    }

}
